<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="使用中" name="1">
          <span slot="label">
            使用中
            <el-tag size="mini">{{ category_count.using }}</el-tag>
          </span>
        </el-tab-pane>
        <el-tab-pane label="待审核" name="0">
          <span slot="label">
            待审核
            <el-tag size="mini">{{ category_count.audit }}</el-tag>
          </span>
        </el-tab-pane>
        <el-tab-pane label="未通过" name="2">
          <span slot="label">
            未通过
            <el-tag size="mini">{{ category_count.no_pass }}</el-tag>
          </span>
        </el-tab-pane>
      </el-tabs>
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="请输入配置名称">
          <el-input
            v-model="formInline.name"
            placeholder="请输入配置名称"
          ></el-input>
        </el-form-item>
        <!--        <el-form-item label="注册时间">-->
        <!--          <div class="block">-->
        <!--            <span class="demonstration"></span>-->
        <!--            <el-date-picker v-model="formInline.create_time" type="daterange" value-format="yyyy-MM-dd"-->
        <!--                            range-separator="至"-->
        <!--                            start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>-->
        <!--          </div>-->
        <!--        </el-form-item>-->
        <el-form-item label="配置分类" prop="express_id">
          <el-select
            size="small"
            v-model="formInline.type"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in category"
              :key="index"
              :label="item.value"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="mb16">
        <div class="common-level-rail fl">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            @click="addClick"
            >添加配置</el-button
          >
          <el-button
            v-if="activeName != 1"
            :disabled="this.multipleSelection.length == 0"
            size="small"
            type="primary"
            icon="el-icon-coin"
            @click="batchRecommend"
            >批量审核</el-button
          >
        </div>
      </div>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            v-if="Number(activeName) != 1"
            key="2"
            type="selection"
            width="55"
          />
          <el-table-column prop="id" label="ID" width="80"></el-table-column>
          <el-table-column prop="image" label="配置图片">
            <template slot-scope="scope">
              <img :src="scope.row.image" width="30px" height="30px" />
            </template>
          </el-table-column>
          <el-table-column prop="img_size" label="图片尺寸"></el-table-column>
          <el-table-column prop="name" label="配置名称"></el-table-column>
          <el-table-column
            prop="create_time"
            label="创建时间"
            width="140"
          ></el-table-column>
          <el-table-column prop="proxy_id" label="代理商">
            <template slot-scope="scope">
              {{ scope.row.proxy ? scope.row.proxy.name : "--" }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="170">
            <template slot-scope="scope">
              <el-button @click="editClick(scope.row)" type="text" size="small"
                >编辑
              </el-button>
              <el-button
                v-if="scope.row.audit_status != 1"
                @click="auditClick(scope.row)"
                type="text"
                size="small"
                >审核</el-button
              >
              <el-button
                @click="deleteClick(scope.row)"
                type="text"
                size="small"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--审核-->
      <Audit
        v-if="open_audit"
        :open_audit="open_audit"
        :auditform="userModel"
        :title="title"
        :AuditApi="AuditApi"
        :pk="pk"
        :ids="batchIds"
        @closeDialog="closeDialogFunc($event, 'audit')"
      ></Audit>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import DicApi from "@/api/dic.js";
import Audit from "@/components/audit/Audit.vue";
import { deepClone } from "@/utils/base.js";
export default {
  components: {
    Audit
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        name: "",
        phone: ""
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      /*等级*/
      gradeList: [],
      open_tag: false,
      /*所有标签*/
      allTag: [],
      category: [],
      /*切换菜单*/
      activeName: "1",
      /*是否打开审核弹窗*/
      open_audit: false,
      /*分类统计*/
      category_count: {},
      pk: "id",
      title: "配置审核",
      /*接口*/
      AuditApi: DicApi,
      batchIds: [],
      /*选中的数据*/
      multipleSelection: []
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Params.audit_status = self.activeName;
      DicApi.list(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
          self.category = data.data.category;
          self.category_count = data.data.category_count;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*搜索查询*/
    onSubmit() {
      let self = this;
      self.loading = true;
      let Params = self.formInline;
      self.getTableList();
    },

    /*打开添加*/
    addClick(item) {
      this.$router.push({
        path: "/setting/dic/edit",
        query: {
          id: 0
        }
      });
    },
    /*打开编辑*/
    editClick(item) {
      let self = this;
      let params = item.id;
      this.$router.push({
        path: "/setting/dic/edit",
        query: {
          id: params
        }
      });
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "audit") {
        this.open_audit = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
    },

    /*删除用户*/
    deleteClick(row) {
      let self = this;
      self
        .$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          self.loading = true;
          DicApi.delete(
            {
              id: row.id
            },
            true
          )
            .then(data => {
              self.loading = false;
              if (data.code == 1) {
                self.$message({
                  message: "恭喜你，删除成功",
                  type: "success"
                });
                self.getTableList();
              } else {
                self.loading = false;
              }
            })
            .catch(error => {
              self.loading = false;
            });
        })
        .catch(() => {
          self.loading = false;
        });
    },
    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getTableList();
    },
    /*打开审核*/
    auditClick(item) {
      this.userModel = deepClone(item);
      this.open_audit = true;
    },
    //记录选中数值
    handleSelectionChange(val) {
      this.multipleSelection = val;
      const data = [];
      this.multipleSelection.map(item => {
        data.push(item[this.pk]);
      });
      this.batchIds = data;
    },
    batchRecommend() {
      this.userModel = {};
      this.open_audit = true;
    }
  }
};
</script>
<style></style>
